package com.wang.test.demo.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;


public class AesUtil {

	static String s = "这是我不想要的";

	/**
	 * 生成秘钥
	 * @return
	 */
	public static String generateSecretKey (){
		byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
		return HexUtil.encodeHexStr(key);
	}

	/**
	 * 根据秘钥和内容进行加密成base64
	 * @param content
	 * @param secretKey
	 * @return
	 */
	public static String encryption(String content,String secretKey){
		byte[] bytes = HexUtil.decodeHex(secretKey);
		AES aes = SecureUtil.aes(bytes);
		String encryptHex = URLUtil.encodeAll(aes.encryptBase64(content));
		return encryptHex;
	}

	/**
	 * 根据秘钥和内容进行解密
	 * @param content
	 * @param secretKey
	 * @return
	 */
	public static String decrypt(String content,String secretKey){
		byte[] bytes = HexUtil.decodeHex(secretKey);
		AES aes = SecureUtil.aes(bytes);
		// URLUtil.decode(content)
		String decryptStr = aes.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
		return decryptStr;
	}

	/**
	 * 根据秘钥和内容进行解密(测试使用)
	 * @param content
	 * @param secretKey
	 * @return
	 */
	public static String decryptTest(String content,String secretKey){
		byte[] bytes = HexUtil.decodeHex(secretKey);
		AES aes = SecureUtil.aes(bytes);
		String decryptStr = aes.decryptStr(URLUtil.decode(content), CharsetUtil.CHARSET_UTF_8);
		return decryptStr;
	}
}
